<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once 'model/db2.php';


?>

<h1>Nguyễn Quốc Thông</h1>
<?php 
    $query = "SELECT * FROM companies";
    $companies = getData($query);
    $query = "SELECT * FROM apps";
    $apps=  getData($query);
 ?>
<form method="POST">
    Min fans<input type="text" name="min_fans"><br>
    Max fans<input type="text" name="max_fans"><br>
   Từ Khóa   <input type="text" name="keyword"><br>
    Công TY<select name="id_company">
        <option><?php echo "------Company------" ?></option>
        <?php foreach ($companies as $company): ?>
        <option value="<?php echo $company['id'] ?>"><?php echo $company['name']?></option>
        <?php        endforeach;?>
    </select><br>
    Ứng Dụng :  <select name ='id_app'>
        <option><?php echo "------App------" ?></option>
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    <input type="submit" name="submit">
</form>

<?php if (isset($_POST['submit'])):?>
    <?php 
        $min_fans =(double) $_POST['min_fans'];
        $max_fans = (double)$_POST['max_fans'];
        $keyword = $_POST['keyword'];
        $query = " SELECT *
            FROM pages
            INNER JOIN apps_pages ON apps_pages.id_page = pages.id
            INNER JOIN apps ON apps.id = apps_pages.id_app
                      WHERE (apps.id_company=id_company) AND (apps.id=id_app) AND (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                      LIMIT 0, 10
                  ";
        $pages = getData($query);
    ?>
    <?php if (!empty($pages)):?>
    <table id="table1" border="1px">
        <tr class="khung">
        <td class="pid">STT </td>
        <td class="picture">Hình Ảnh Đại Diện </td>
        <td class="id">Số id </td>
        <td class="name">Tên Fans page </td>
        <td class="likes">Lượt Thích </td>
        <td class="talking">Lượt nhắc đến</td>
        <td class="category">Lĩnh Vực </td>
        <td class="link">Link Fans Page </td>
        <td class="website">Website </td>
        <td class="country">Quốc Gia </td>
        </tr>
        <?php foreach ($pages as $page):?>
                     <tr>
                 <td><?php echo $page["pid"] ?></td>
                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td><?php echo $page["country"] ?></td>
          
            </tr>
            
        <?php endforeach; ?>
            
    </table> 
<?php endif;?>
<?php endif;?>
</table>